package host.skiree.nettydemo;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author gcl
 * @date 2022/9/8 8:26
 */
@Component
public class TaskApplication implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        new Thread(new Task("Thread -- 1")).start();
        new Thread(new Task("Thread -- 2")).start();
        new Thread(new Task("Thread -- 3")).start();
        new Thread(new Task("Thread -- 4")).start();
        new Thread(new Task("Thread -- 5")).start();
        new Thread(new Task("Thread -- 6")).start();
        new Thread(new Task("Thread -- 7")).start();
        new Thread(new Task("Thread -- 8")).start();
    }

    static class Task implements Runnable{
        private String name;

        private ConcurrentLinkedQueue<Runnable> queue = new ConcurrentLinkedQueue<>();

        public Task(String name){
            this.name = name;
            queue.add(() -> {
                toString();
            });
        }

        public String toString(){
            System.out.println("线程 "+ this.name+" 启动");
            return null;
        }

        @Override
        public void run() {
            Runnable task = queue.poll();
            if ( task != null ){
                task.run();
            }
            while (true){
                for (int i = 1; i < 10000001; i++) {
                    SpringContextHolder.getApplicationContext().getBean(JdbcTemplate.class).execute("INSERT INTO `data`.`data`(`name`, `birth`) VALUES ('" + RandomUtil.genChineseName()  +"', '"+RandomUtil.genLocalDate("1949-10-1", "2022-9-7")+"');");
                }
            }
        }
    }

}
